package com.infore.airsupervise.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.LawingSiteE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.other.listener.LayoutControl;
import com.infore.airsupervise.other.listener.LayoutControlListener;
import com.infore.airsupervise.ui.activity.SitePartDetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 执法台账 检查笔录 界面
 * Created by xiaotao on 2017/5/31.
 */

public class SiteInspectDetailFragment extends BaseFragment {
    @BindView(R.id.tv_info_arrow)
    AwesomeTextView tvInfoArrow;
    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_inspect_pson)
    TextView tvInspectPson;
    @BindView(R.id.tv_note_pson)
    TextView tvNotePson;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_leader_name)
    TextView tvLeaderName;
    @BindView(R.id.et_leader_age)
    TextView etLeaderAge;
    @BindView(R.id.tv_identify)
    TextView tvIdentify;
    @BindView(R.id.tv_source)
    TextView tvSource;
    @BindView(R.id.tv_position)
    TextView tvPosition;
    @BindView(R.id.tv_relationship)
    TextView tvRelationship;
    @BindView(R.id.tv_address_inspect)
    TextView tvAddressInspect;
    @BindView(R.id.tv_postCode)
    TextView tvPostCode;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_other)
    TextView tvOther;
    @BindView(R.id.tv_evite)
    TextView tvEvite;
    @BindView(R.id.tl_info)
    TableLayout tlInfo;
    @BindView(R.id.tv_condition_arrow)
    AwesomeTextView tvConditionArrow;
    @BindView(R.id.rv_askModel)
    RecyclerView rvAskModel;

    private SitePartDetailActivity parent;
    private LawingSiteE.NotesJC inspectE;

    private LayoutControlListener layoutControl;
    private BaseQuickAdapter<LawingSiteE.NoteModel,BaseViewHolder> adapter ;

    public SiteInspectDetailFragment() {
    }

    @SuppressLint("ValidFragment")
    public SiteInspectDetailFragment(Object result) {
        inspectE = (LawingSiteE.NotesJC) result;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_site_inspect_detail, container, false);
        unbinder = ButterKnife.bind(this, view);
        parent = (SitePartDetailActivity) getActivity();
        layoutControl = new LayoutControl(parent);
        setInspectData();
        return view;
    }

    private void setInspectData(){
        if(null == inspectE){
            return ;
        }
        tvStartTime.setText(inspectE.getStartTime());
        tvEndTime.setText(inspectE.getEndTime());
        tvAddress.setText(inspectE.getAddress());
        tvLeaderName.setText(inspectE.getLeaderName());
        etLeaderAge.setText(inspectE.getLeaderAge());
        tvIdentify.setText(inspectE.getIdentify());
        tvRelationship.setText(inspectE.getRelationship());
        tvPosition.setText(inspectE.getPosition());
        tvPostCode.setText(inspectE.getPostCode());
        tvPhone.setText(inspectE.getPhone());
        tvOther.setText(inspectE.getOthers());
        tvEvite.setText(inspectE.getEvite() == 0 ? "是": "否");
        if(null != inspectE.getNoteModels() && inspectE.getNoteModels().size()>0 ){
            initRecyclerView(inspectE.getNoteModels());
        }

        //检查人员
        if(null != inspectE.getInspectPson() && inspectE.getInspectPson().size()>0){
            StringBuffer str_inspectPson = new StringBuffer();
            for(UserE userE : inspectE.getInspectPson()){
                str_inspectPson.append(userE.getRealName())
                        .append(",");
            }
            str_inspectPson.deleteCharAt(str_inspectPson.length()-1);
            tvInspectPson.setText(str_inspectPson);
        }
        //记录人
        if(null != inspectE.getNotePson() && inspectE.getNotePson().size() > 0){
            StringBuffer str_notePson = new StringBuffer();
            for(UserE userE : inspectE.getInspectPson()){
                str_notePson.append(userE.getRealName())
                        .append(",");
            }
            str_notePson.deleteCharAt(str_notePson.length()-1);
            tvNotePson.setText(str_notePson);
        }

    }

    private void initRecyclerView( List<LawingSiteE.NoteModel> modelList){
        adapter = new BaseQuickAdapter<LawingSiteE.NoteModel, BaseViewHolder>(R.layout.item_site_inspectmodel_show,modelList) {
            @Override
            protected void convert(BaseViewHolder helper, LawingSiteE.NoteModel item) {
                helper.setText(R.id.tv_question,item.getQuestion())
                        .setText(R.id.tv_answer, item.getAnswer());
            }
        };
        rvAskModel.setLayoutManager( new LinearLayoutManager(parent));
        rvAskModel.setAdapter(adapter);
    }


    boolean isInfoShow;
    boolean isConditionShow;
    @OnClick({R.id.ll_info_control, R.id.ll_condition_control})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_info_control:
                isInfoShow = !isInfoShow;
                layoutControl.layoutControl(isInfoShow,tlInfo,tvInfoArrow);
                break;
            case R.id.ll_condition_control:
                isConditionShow = !isConditionShow;
                layoutControl.layoutControl(isConditionShow,rvAskModel,tvConditionArrow);
                break;
        }
    }
}
